package com.woniuxy.house.mapper;

import com.woniuxy.house.entity.Comment;
import com.woniuxy.house.provider.CommentSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 评论表mapper
 */
public interface CommentMapper {

    /**
     * 增加评论表
     */
    @Insert("insert into house_comment(id,time,content,user_id,post_id) values(#{id},#{time},#{content},#{user.id},#{post.id})")
    void addComment(Comment comment);


    /**
     * 修改评论表
     */
    @UpdateProvider(type = CommentSqlProvider.class,method = "update")
    void updateComment(Comment comment);


    /**
     * 删除评论表
     */
    @Delete("DELETE FROM house_comment WHERE id=#{id}")
    void deleteComment(Integer id);


    /**
     * 查询全部的评论表
     */
    @Select("select * from house_comment")
    @Results(id = "result",value = {
            @Result(column = "id",property = "id",id = true),
            @Result(column = "time",property = "time"),
            @Result(column = "content",property = "content"),
            @Result(column = "user_id",property = "user",one = @One(select = "com.woniuxy.house.mapper.UserMapper.getById",fetchType = FetchType.LAZY)),
            @Result(column = "post_id",property = "post",one = @One(select = "com.woniuxy.house.mapper.PostMapper.getById",fetchType = FetchType.LAZY))

    })
    List<Comment> getAll();

    /**
     * 多条件查询
     */
    @SelectProvider(type = CommentSqlProvider.class,method="select")
    @ResultMap("result")
    List<Comment> getByCondition(@Param("account") String account,@Param("title") String title );

    /**
     * 根据用户id查询的评论表
     */
    @Select("select * from house_comment where user_id=#{id}")
    @ResultMap(value = "result")
    List<Comment> getByUserId(Integer userId);

    /**
     * 根据帖子id查询的评论表
     */
    @Select("select * from house_comment where post_id=#{id}")
    @ResultMap(value = "result")
    List<Comment> getByPostId(Integer postId);
}
